﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Toolkit.Web.Helpers
{
    /// <summary>
    /// Base64 Encoding Helper.
    /// Helper class to handle encoding and decoding of Base64 strings.
    /// </summary>
    public static class Base64Encoding
    {
        /// <summary>
        /// Method to create a Base64 encoded string from a normal string.
        /// </summary>
        /// <param name="value">String to encode.</param>
        /// <returns>Base64 encoded string.</returns>
        public static string Encode(string value)
        {
            return Encode(value, null);
        }
 
        /// <summary>
        /// Method to create a Base64 encoded string from a normal string.
        /// </summary>
        /// <param name="value">String to encode.</param>
        /// <param name="encoding">Type of encoding to use.</param>
        /// <returns>Base64 encoded string.</returns>
        public static string Encode(string value, Encoding encoding)
        {
            if (encoding == null)
            {
                encoding = Encoding.Default;
            }
            byte[] valueAsBytes = encoding.GetBytes(value);
            return Convert.ToBase64String(valueAsBytes);
        }
 
        /// <summary>
        /// Method to decode Base64 encoded strings.
        /// </summary>
        /// <param name="value">String to decode.</param>
        /// <returns>Decoded string.</returns>
        public static string Decode(string value)
        {
            return Decode(value, null);
        }
 
        /// <summary>
        /// Method to decode Base64 encoded strings.
        /// </summary>
        /// <param name="value">String to decode.</param>
        /// <param name="encoding">Type of encoding to use.</param>
        /// <returns>Decoded string.</returns>
        public static string Decode(string value, Encoding encoding)
        {
            if (encoding == null)
            {
                encoding = Encoding.Default;
            }
            byte[] valueAsBytes = Convert.FromBase64String(value);
            return encoding.GetString(valueAsBytes);
        }
    }
}
